<?php
    namespace Home\Controller;
    use Think\Controller;
    class ChengbenxiangmushezhiController extends Controller {
    
    public function _initialize()
    {
        header("Access-Control-Allow-Origin: *");  

        header("Access-Control-Allow-Credentials: true"); 
      
        header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS"); //允许的请求类型
        
        header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 

    }

    public function get_list()
    {
        $page = I("get.page");
        $limit = I("get.limit");
        $cost_item_code = I("get.cost_item_code");
        $where = "1 = 1 ";

        if(!empty($cost_item_code))
        {
            $where .= " and COST_TYPE_CODE = '$cost_item_code'";
        }

        $data = M("cbhs.cbhs_cost_item_dict")->where($where)->limit(($page-1)*$limit,$limit)->select();

        for ($i=0; $i < count($data); $i++) 
        { 
            $info = $data[$i];
            
        }
        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("cbhs.cbhs_cost_item_dict")->where($where)->count();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function get_cbhs_cost_type_dict_list()
    {
        $data = M("cbhs.cbhs_cost_type_dict")->select();

        for ($i=0; $i < count($data); $i++) 
        { 
            $info = $data[$i];
        }
        $json["data"] = $data;
        $json["code"] = 0;
        $json["count"] = M("cbhs.cbhs_cost_type_dict")->count();
        $json["msg"] = "";
        
        $this->ajaxReturn($json,"json");
    }

    public function get_one()
    {
        $cost_item_code = I("post.cost_item_code");

        $info = M("cbhs.cbhs_cost_item_dict")->where("cost_item_code = '$cost_item_code'")->find();
        $json["data"]   = $info;
        $json["code"]   = 0;
        $json["msg"]    = "";

        $this->ajaxReturn($json,"json");
    }

    public function save_one()
    {
        $data = I("post.");
        $cost_item_code = $data["cost_item_code"];
        unset($data["cost_item_code"]);
        $result = M("cbhs.cbhs_cost_item_dict")->where("cost_item_code = '$cost_item_code'")->save($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "修改成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "修改失败";
        }
 
        $this->ajaxReturn($json,"json");
    }

    public function add_one()
    {
        $data = I("post.");
        
        $result = M("cbhs.cbhs_cost_item_dict")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }


    public function add_type()
    {
        $data = I("post.");
        
        $cost_item_code = $data['cost_item_code'];

        $info = M("cbhs.cbhs_cost_type_dict")->where("COST_TYPE_CODE = '$cost_item_code'")->find();

        if(empty($info))
        {
            $json["code"] = -1;
            $json["msg"] = "类别编码重复";
            $this->ajaxReturn($json,"json");
        }

        $result = M("cbhs.cbhs_cost_type_dict")->add($data);
        if($result !== false)
        {
            $json["code"] = 0;
            $json["msg"] = "添加成功";
        }
        else
        {
            $json["code"] = -1;
            $json["msg"] = "添加失败";
        }
 
        $this->ajaxReturn($json,"json");
    }



   }